package com.whfc.wxmp.api.emp.service.impl;

import com.whfc.common.exception.BizException;
import com.whfc.common.face.aiot.AiotApiResult;
import com.whfc.common.face.aiot.AiotCode;
import com.whfc.common.face.aiot.AiotFaceApi;
import com.whfc.common.file.FileHandler;
import com.whfc.common.file.FilePathConfig;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageVO;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.ImageUtil;
import com.whfc.common.util.RandomUtil;
import com.whfc.emp.dto.*;
import com.whfc.emp.param.AppEmpAddParam;
import com.whfc.emp.param.AppEmpDeviceBindParam;
import com.whfc.emp.param.AppEmpEditParam;
import com.whfc.emp.service.AppEmpService;
import com.whfc.emp.service.AppEmpWorkService;
import com.whfc.entity.dto.WxDeviceBindCheckDTO;
import com.whfc.fuum.dto.SysDeptDTO;
import com.whfc.fuum.entity.WxUser;
import com.whfc.fuum.service.SysDeptService;
import com.whfc.wxmp.api.base.service.WxOptionService;
import com.whfc.wxmp.api.emp.service.WxEmpService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/8/27 9:15
 */
@Service
public class WxEmpServiceImpl implements WxEmpService {

    @DubboReference(interfaceClass = AppEmpService.class, version = "1.0.0")
    private AppEmpService appEmpService;

    @DubboReference(interfaceClass = AppEmpWorkService.class, version = "1.0.0")
    private AppEmpWorkService appEmpWorkService;

    @DubboReference(interfaceClass = SysDeptService.class, version = "1.0.0")
    private SysDeptService sysDeptService;

    @Autowired
    private WxOptionService optionService;

    @Autowired
    private FileHandler fileHandler;

    @Autowired
    private FilePathConfig filePathConfig;
    @Override
    public ListData<WxEmpGroupDTO> getEmpList(Integer deptId, String keyword) {
        List<WxEmpGroupDTO> list = appEmpService.getEmpList(deptId, keyword);
        return new ListData<>(list);
    }

    @Override
    public List<AppEmpDTO> getLeaderList(Integer deptId, String keyword) {
        return appEmpService.getLeaderList(deptId, keyword);
    }

    @Override
    public WxMapEmpDTO getMapList(Integer deptId) {
        return appEmpService.getMapList(deptId);
    }

    @Override
    public ListData<AppWorkTypeDTO> workTypeList(Integer deptId) {
        List<AppWorkTypeDTO> list = appEmpWorkService.workTypeList(deptId);
        return new ListData<>(list);
    }

    @Override
    public AppEmpDTO getEmpInfo(WxUser wxUser, Integer empId) {
        return appEmpService.detail(empId);
    }

    @Override
    public void editEmpInfo(WxUser wxUser, AppEmpEditParam request) {
        appEmpService.edit(request);
    }

    @Override
    public void delEmp(Integer empId) {
        appEmpService.del(empId);
    }

    @Override
    public void bindDevice(WxUser user, AppEmpDeviceBindParam request) {
        appEmpService.bind(user.getName(), user.getId(), request);
    }

    @Override
    public void unbindDevice(WxUser user, Integer empId) {
        appEmpService.unbindEmpDevice(user.getId(), user.getName(), empId);
    }

    @Override
    public void bindEmp(WxUser user, DeviceBindEmp request) {
        appEmpService.bindAndAddEmp(user.getId(), user.getName(), request);
    }

    @Override
    public WxDeviceBindCheckDTO bindCheck(String sn) {
        DeviceBindCheckDTO deviceBindCheckDTO = appEmpService.bindCheck(sn);
        WxDeviceBindCheckDTO wxDeviceBindCheckDTO = new WxDeviceBindCheckDTO();
        BeanUtils.copyProperties(deviceBindCheckDTO, wxDeviceBindCheckDTO);
        return wxDeviceBindCheckDTO;
    }


    @Override
    public WxEmpWorkHourDTO getEmpWorkHour(WxUser wxUser, Integer empId, Date date) {
        return appEmpService.getEmpWorkHour(empId, date);
    }

    @Override
    public ListData<AppEmpGpsDTO> gpsLog(Integer empId, Date date) {
        List<AppEmpGpsDTO> gpsList = appEmpService.getGpsList(empId, date);
        return new ListData<>(gpsList);
    }

    @Override
    public AppEmpGpsDTO getLatestGps(Integer empId) {
        return appEmpService.getLatestGps(empId);
    }

    @Override
    public WxEmpDataCenterDTO getEmpDataCenter(Integer deptId) {
        return appEmpService.getEmpDataCenter(deptId);
    }


    @Override
    public WxMapEmpDTO getMapListNew(Integer deptId, List<Integer> groupIds, List<Integer> corpIds,String name) {

        WxMapEmpDTO mapList = appEmpService.getMapListNew(deptId,groupIds,corpIds,name);
        return mapList;
    }

    @Override
    public PageVO<AppEmpGpsDTO> gpsLogPage(Integer empId, Date date, Integer pageNum, Integer pageSize) {
        PageVO<AppEmpGpsDTO> gpsList = appEmpService.getGpsListPage(empId, date,pageNum,pageSize);
        return gpsList;
    }

    @Override
    public List<AppCorpGroupDTO> getEmpListV2(Integer deptId, String keyword) {
        List<AppCorpGroupDTO> appCorpGroupDTOS = optionService.corpGroupList(deptId);
        WxMapEmpDTO mapList = appEmpService.getMapList(deptId);
        List<WxEmpGroupDTO> list = appEmpService.getEmpList(deptId, keyword);
//        List<WxEmpGroupDTO> list = mapList.getList();
        appCorpGroupDTOS.stream().forEach(appCorpGroupDTO -> {
            List<AppGroupDTO> groupList = appCorpGroupDTO.getGroupList();
            for (AppGroupDTO group:groupList) {
                Integer groupId = group.getGroupId();
                List<AppEmpDTO> empList = new ArrayList<>();
                for (WxEmpGroupDTO groupDTO:list) {
                    Integer groupId1 = groupDTO.getGroupId();
                    if(groupId.equals(groupId1)){
                        empList.addAll(groupDTO.getDataList());
                        BeanUtils.copyProperties(groupDTO,group);
                    }
                }
                group.setEmpList(empList);
            }
        });
        mapList.setCorpGroupEmpList(appCorpGroupDTOS);
        return appCorpGroupDTOS;
    }

    @Override
    public String uploadImg(String base64Str, MultipartFile file, Boolean faceFlag) throws IOException {
        String path = filePathConfig.getFilePath("emp/image", RandomUtil.getRandomFileName(), "png");
        if (file != null) {
            //文件上传
            InputStream inputStream = file.getInputStream();
            String upload = fileHandler.upload(path, inputStream);
            if (faceFlag != null && faceFlag) {
                AiotApiResult<String> result = AiotFaceApi.detect(upload, null);
                if (!AiotCode.OP_SUS_1000.getValue().equals(result.getCode())) {
                    String message = AiotCode.parseByValue(result.getCode());
                    throw new BizException(ResultEnum.FAILURE.getCode(), message);
                }
            }
            return upload;
        } else if (!StringUtils.isBlank(base64Str)) {
            if (faceFlag != null && faceFlag) {
                AiotApiResult<String> result = AiotFaceApi.detect(null, base64Str);
                if (!AiotCode.OP_SUS_1000.getValue().equals(result.getCode())) {
                    String message = AiotCode.parseByValue(result.getCode());
                    throw new BizException(ResultEnum.FAILURE.getCode(), message);
                }
            }
            //base64上传
            byte[] imageByte = ImageUtil.base64ToImage(base64Str);
            ByteArrayInputStream inputStream = new ByteArrayInputStream(imageByte);
            return fileHandler.upload(path, inputStream);
        }
        throw new BizException(ResultEnum.FAILURE.getCode(), "参数错误");
    }


    @Override
    public void add(AppEmpAddParam request) {
        appEmpService.add(request);
    }

    @Override
    public List<AppEmpDTO> getEmpListByPhone(String phone) {
        return appEmpService.getListByPhone(phone);
    }

}
